package com.example.myproject.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;

public class SelectionPanel extends DockPanel{

	//Reference to the current list of modules
	Modules modList;
	
	DisplayPanel display;
	Button configButton;
	Button calcButton;
	Button displayButton;
	Button mapButton;
	Button weather;
	
	/*Selection panel is the panel that holds all the options for the module
	 * it allows you to congifure a module, display the map, find configurations...etc.
	 */
	SelectionPanel(Modules moduleList)
	{
		modList = moduleList;
		
		//set vars
		display = new DisplayPanel(moduleList);
		this.setSize("802px", "200px");		
		this.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
	    this.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		
	    //add the configure a module button
		configButton = new Button("Configure Selected Module");		
		//dont enable it yet, only enable when a module is selected
		configButton.setEnabled(false);		
		configButton.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	//set the display panel to the module
	        	display.SetDisplay(1, modList.getSelectedModule());
	        	//check for a minimum configuration
	        	checkMin();
	        }
	    });
		
		//add the calculate config button
		calcButton = new Button("Calculate and Display Possible Module Configurations");
		//dont enable it yet until a min config is reached
		calcButton.setEnabled(false);
		calcButton.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	//popup a panel diplaying configurations
	        	popupMin();
	        }
	    });
		
		//create a button to display rover path to module
		displayButton = new Button("Display Rover Path To Selected Module");
		//dont enable it yet
		displayButton.setEnabled(false);
		
		
		//creat a button to show module map
		mapButton = new Button("Display Module Map");
		mapButton.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	//create a popup for the map when clicked
	        	Voices.playConfiguredMod();
	        	popupMap();
	        }
	    });

		weather = new Button("Display Current Weather Conditions");
		weather.setEnabled(true);
		weather.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event){
				weather x = new weather();
				x.getWeather();
			}
			
		});	

		this.add(weather, DockPanel.WEST);

		//add all the buttons
		this.add(configButton, DockPanel.WEST);
		this.add(mapButton, DockPanel.WEST);
		this.add(calcButton, DockPanel.WEST);
		this.add(displayButton, DockPanel.WEST);
		this.add(weather, DockPanel.WEST);
		
		this.setStyleName("borderStyle");
	}
	
	//this is called when a module has been selected, but it enables display map button and config
	void notifySelected()
	{
		displayButton.setEnabled(true);
		configButton.setEnabled(true);
	}
	
	//get the display panel
	DisplayPanel getDisplay()
	{
		
		return display;
	}
	
	//popup a panel to display possible configurations
	void popupMin()
	{
		//create a popup panel
		final PopupPanel pop = new PopupPanel();
		pop.setSize("750px","550px");
		pop.setStyleName("popupStyle");	
		//create a panel for the conents and init the panel
		DockPanel popupContents = new DockPanel();
		popupContents.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		popupContents.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);		
		
		//create a button to close the popup
		Button okButton = new Button("OK");
		okButton.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	pop.hide();	
	        }
	    });		
		//create a panel that is the congurations from module list
		Panel addme = modList.getConfigPanel();
		//add everything to the contents
		popupContents.add(addme, DockPanel.NORTH);
		popupContents.add(okButton, DockPanel.NORTH);
		
		//add the contents to the popup
		pop.add(popupContents);
		
		//dim everything else
		pop.setGlassEnabled(true);
		//set the position and show it
		pop.setPopupPosition(20, 20);
		pop.show();
	}
	
	//crete a popup panel to display the module map
	void popupMap()
	{
		//create the popup panel
		final PopupPanel pop = new PopupPanel();
		pop.setSize("750px","550px");
		pop.setStyleName("popupStyle");
		//create the popup content panel
		DockPanel popupContents = new DockPanel();	
		popupContents.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		popupContents.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
				
		//create a scroll panel for the map
		ScrollPanel scroll = new ScrollPanel();
		scroll.add(modList.getMap());
		//get the map from the module list
		scroll.setSize("735px", "500px");
		
		//create a button to close the popup
		Button okButton = new Button("OK");
		okButton.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	//hide the popup
	        	pop.hide();	
	        }
	    });		
		
		//add everything to the popup content panel
		popupContents.add(scroll, DockPanel.NORTH);
		popupContents.add(okButton, DockPanel.NORTH);
		
		//add popup content panel to popup
		pop.add(popupContents);
		
		//dim everything, set the position and show popup
		pop.setGlassEnabled(true);
		pop.setPopupPosition(20, 20);
		pop.show();
	}
	
	void checkMin()
	{
		//check for a minimum config
		modList.checkMinConfig();
		if(modList.minConfig)
			//if it is minimum config, enable the calculate configurations button
			calcButton.setEnabled(true);
	}
}
